<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryForm" :inline="true">
        <!-- StartDate  EndDate -->
        <el-form-item prop="keywords">
          <el-input v-model="queryForm.keywords" placeholder="请输入姓名/企业名称" clearable style="width: 200px" />
          <el-button type="primary" style="margin-left: 0; border-radius: 0" @click="handleQuery"> 搜索 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-container">
      <el-row :gutter="20">
        <el-col :span="24" :offset="0">
          <el-button v-if="permissions.create" type="primary" size="default" @click="Create">新增</el-button>
          <!-- <el-button v-if="permissions.renewal" type="primary" size="default" @click="Renewal">续签</el-button> -->
          <!-- <el-button  v-if="permissions.isCreate" size="default" @click="Export">导出</el-button>
            <el-button  v-if="permissions.isCreate" size="default" @click="BatchImport">批量导入</el-button> -->
        </el-col>
        <el-col :span="24" :offset="0" style="margin-top: 10px">
          <el-table
            ref="tableRef"
            v-loading="loading"
            :data="tableData"
            style="width: 100%"
            :show-overflow-tooltip="{ effect: 'light' }"
            @select="handleCurrentChange"
          >
            <el-table-column type="selection" align="center" width="50" />
            <el-table-column label="合同编号" prop="code" min-width="120" />
            <el-table-column label="姓名" prop="personName" min-width="100" />
            <el-table-column label="证件号码" prop="cardNumber" min-width="120" />
            <el-table-column label="所属企业" prop="corpName" min-width="120" />
            <el-table-column label="合同类型" prop="typeName" min-width="80" />
            <el-table-column label="合同签订日期" prop="signingDate" min-width="100" />
            <el-table-column label="合同终止日期" prop="endDate" min-width="100" />
            <el-table-column label="工资计算方式" prop="salaryCalculationMethodName" min-width="80" />
            <el-table-column label="工资标准" prop="wageStandard" min-width="80" />
            <el-table-column label="工资支付日期" prop="salaryPaymentDate" min-width="100" />
            <el-table-column label="操作" fixed="right" width="160">
              <template #default="scope">
                <el-button v-if="permissions.update" link size="small" @click="Edit(scope.row)"> 修改 </el-button>
                <!-- <el-button v-if="permissions.delete" type="danger" link size="small" @click="Delete(scope.row)">
                  删除
                </el-button> -->
                <el-button v-if="permissions.detail" type="primary" link size="small" @click="handleDetail(scope.row)">
                  详情
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-if="total > 0"
            v-model:total="total"
            v-model:page="queryForm.pageIndex"
            v-model:limit="queryForm.pageSize"
            @pagination="handleQuery"
          />
        </el-col>
      </el-row>
    </div>
    <add-form v-if="dialogAddOrEdit.visible" :dialog-add-or-edit="dialogAddOrEdit" @send-close="sendClose" />
    <detail v-if="dialogDetail.visible" :dialog-detail="dialogDetail" @send-close="sendClose" />
  </div>
</template>
<script setup lang="ts">
import addForm from "./addForm.vue";
import detail from "./detail.vue";
import { QueryPersonContractPage, DeletePersonContract } from "@/api/contract/index";

const permissions = ref({
  create: false,
  update: false,
  delete: false,
  detail: false,
  renewal: false,
});

const currentSystem = ref(localStorage.getItem("usertypeName"));
const isLumpContractor = ref(localStorage.getItem("isLumpContractor"));

if (currentSystem.value == "SysUser") {
  permissions.value.detail = true;
}
if (currentSystem.value == "Commander") {
  permissions.value.detail = true;
}
if (currentSystem.value == "Constructor") {
  permissions.value.detail = true;
}
if (currentSystem.value == "Corp") {
  permissions.value = {
    create: true,
    update: true,
    delete: true,
    detail: true,
    renewal: true,
  };
}
if (currentSystem.value == "Manager") {
  permissions.value = {
    create: true,
    update: true,
    delete: true,
    detail: true,
    renewal: true,
  };
}

const loading = ref(false);
const tableData = ref();

const dialogAddOrEdit = reactive<DialogOption>({
  visible: false,
});
const dialogDetail = reactive({
  visible: false,
  title: "",
  id: undefined,
  projectId: undefined,
  corpId: undefined,
});
//当前选中项
const currentItem = ref();
const tableRef = ref();

//实现复选框的单选
const handleCurrentChange = function (selections: any, row: any) {
  if (selections.length > 1) {
    // 把数组的第一个元素从其中删除
    let del_row = selections.shift();
    // 用于多选表格，切换某一行的选中状态，如果使用了第二个参数，则是设置这一行选中与否（selected 为 true 则选中）
    tableRef.value.toggleRowSelection(del_row, false);
    currentItem.value = selections[0];
  } else {
    currentItem.value = selections[0];
  }
};

//新增
const Create = function () {
  dialogAddOrEdit.visible = true;
  dialogAddOrEdit.footBtn = "确认新增";
  dialogAddOrEdit.title = "新增";
};
//编辑
const Edit = function (row: any) {
  dialogAddOrEdit.visible = true;
  dialogAddOrEdit.id = row.id;
  dialogAddOrEdit.footBtn = "确认修改";
  dialogAddOrEdit.title = "修改";
};
const Renewal = function (row: any) {
  if (currentItem.value?.id == undefined || currentItem.value.id <= 0) {
    ElMessage({
      type: "error",
      message: "请选择合同",
    });
    return;
  }
  dialogAddOrEdit.visible = true;
  dialogAddOrEdit.id = currentItem.value.id;
  dialogAddOrEdit.footBtn = "确认续签";
  dialogAddOrEdit.title = "续签";
};

//详情
const handleDetail = function (row: any) {
  dialogDetail.visible = true;
  dialogDetail.id = row.personId;
  dialogDetail.title = "详情";
  dialogDetail.projectId = row.projectId;
  dialogDetail.corpId = row.corpId;
};

const sendClose = function (needRefresh: boolean) {
  dialogAddOrEdit.visible = false;
  dialogAddOrEdit.id = undefined;
  dialogDetail.visible = false;
  dialogDetail.id = undefined;
  if (needRefresh) {
    handleQuery();
  }
};

//删除
const Delete = function (row: any) {
  ElMessageBox.confirm("确定删除该合同吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      DeletePersonContract(row.id).then((res) => {
        if (res.type === 0) {
          if (res.success) {
            ElMessage({
              type: "success",
              message: res.msg,
            });
            handleQuery();
          } else {
            ElMessage({
              type: "error",
              message: res.msg,
            });
          }
        }
      });
    })
    .catch(() => {
      console.log("取消");
    });
};

//查询条件
const queryForm = reactive({
  keywords: "",
  pageIndex: 1,
  pageSize: 10,
});
const total = ref(0);

//这是列表数据
const handleQuery = function () {
  loading.value = true;
  QueryPersonContractPage(queryForm)
    .then((res) => {
      if (res.type === 0) {
        if (res.success) {
          tableData.value = res.data.list;
          queryForm.pageIndex = res.data.pageNum;
          queryForm.pageSize = res.data.pageSize;
          total.value = res.data.total;
        } else {
        }
      } else {
      }
    })
    .finally(() => {
      loading.value = false;
    });
};

onMounted(() => {
  handleQuery();
});
</script>
<style scoped>
:deep(.el-button) {
  margin-left: 5px;
}

:deep(.el-input__wrapper) {
  border-radius: 0;
}
</style>
